import { Component } from '@angular/core';

export interface todo {
  id: number;
  content: string;
}

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styles: []
})

export class AppComponent {

  id: number = 0;

  todoList: todo[] = []

  onTodoAdd (todo: string) {
    // 接收add-item的自定义事件todoEmit传递的参数
    if (todo !== '') {
      this.addItem({
        id: this.id ++,
        content: todo
      })
    }
    
  }

  onDeleteItem(id: number) {
    this.removeItem(id);
  }

  addItem (todo: todo) {
    // 添加新的事项
    this.todoList.push(todo);
  }

  removeItem (id: number) {
    // 删除事项
    this.todoList = this.todoList.filter(todo => todo.id !== id);
  }
}
